<?php

// +----------------------------------------------------------------------

namespace app\api\controller;

use app\common\controller\Api;
use app\xianda\model\User as UserModel;
use app\xianda\model\Des as DesModel;
use app\xianda\model\Order as OrderModel;
use app\api\model\Str as StrModel;
use think\Db;
use think\facade\Env;
/**
 * 农商接口控制器
 * @package app\api\controller
 */
class Rcb extends Api
{

	//请求数据接口
	public function send()
	{
		if ($this->request->isPost()) {
			// 获取数据
			$data = $this->request->post('data');
			$data = StrModel::format_str($data);
			if(!$data) return '4    1111';
			switch ($data['type']){
				// BCNotify1 对账请求
				case 'BCNotify1' :
					$file_name = trim(StrModel::mb_substr2($data['body'], 58, 50));  //截取对账文件名
					$file = Env::get('root_path') . 'data' . DIRECTORY_SEPARATOR . 'uptxt' . DIRECTORY_SEPARATOR . $file_name;
					//return $file;
					if(!is_file($file)) {
						$DCC_FLAG = '1111';
						$DCC_RESULT = '对账文件未找到';
					}else{
						// FTP上传
						$ftp = module_config('xianda.ftp');
						if($ftp){
							// 打开文件
							$source = fopen($file,"r");
							$conn = ftp_connect($ftp) or die("Could not connect"); // FTP地址
							ftp_login($conn, module_config('xianda.ftp_user'), module_config('xianda.ftp_pass')); // FTP 账号&密码
							ftp_fput($conn, $file_name, $source,FTP_BINARY);
							ftp_close($conn);
							fclose($source); // 关闭文件
						}
						$DCC_FLAG = '0000';
						$DCC_RESULT = '对账成功';
					}
					$DCC_RESULT = StrModel::addSpace($DCC_RESULT, 20);
					$len = StrModel::addSpace('24', 4);
					$result = $len.$DCC_FLAG.$DCC_RESULT;
				break;
				// 缴费单信息查询（Query Bill）
				case 'Query Bill' :
					$DCC_BILL_KEY = trim(StrModel::mb_substr2($data['body'], 20, 30));// 截取缴费单标示号（客户编号）
					$user = UserModel::where('personlabel', $DCC_BILL_KEY)->find();
					if(!$user) {
						$DCC_FLAG = '1111';
						$DCC_RESULT = '该用户未找到';
						$DCC_RESULT = StrModel::addSpace($DCC_RESULT, 60);
						$len = StrModel::addSpace('64', 4);
						$result = $len.$DCC_FLAG.$DCC_RESULT;
					} else {
						$DCC_FLAG = '0000';
						$DCC_RESULT = '查询成功';
						$DCC_RESULT = StrModel::addSpace($DCC_RESULT, 60);
						$DCC_BILL_KEY = StrModel::addSpace($DCC_BILL_KEY, 30);
						$DCC_COMP_ID = StrModel::mb_substr2($data['body'], 50, 30);
						$DCC_CUS_TEL = StrModel::addSpace($user['personcardno'], 20);
						$DCC_CUS_NAME = StrModel::addSpace($user['personname'], 60);
						$DCC_BAL = StrModel::addSpace(number_format($user['personmoney'], 2, '.', ''), 16);
						$DCC_PAY_AMT = StrModel::addSpace(number_format($user['onlinelimitmoney'], 2, '.', ''), 16);
						$len = StrModel::addSpace('236', 4);
						$result = $len.$DCC_FLAG.$DCC_RESULT.$DCC_BILL_KEY.$DCC_COMP_ID.$DCC_CUS_TEL.$DCC_CUS_NAME.$DCC_BAL.$DCC_PAY_AMT;
					}
				break;
				// 缴费单销账（Bill Charge）
				case 'Bill Charge' :
					$DCC_BILL_KEY = trim(StrModel::mb_substr2($data['body'], 20, 30));// 截取缴费单标示号（客户编号）
					$user = UserModel::where('personlabel', $DCC_BILL_KEY)->find();
					if(!$user) {
						$DCC_FLAG = '1111';
						$DCC_RESULT = '该用户未找到';
						$DCC_RESULT = StrModel::addSpace($DCC_RESULT, 60);
						$len = StrModel::addSpace('64', 4);
						$result = $len.$DCC_FLAG.$DCC_RESULT;
					} else {
						// 实例化加密模型
						$des = new DesModel;
						
						$no = $des->des_cbc_encrypt($DCC_BILL_KEY); // 加密缴费用户编号
						
						$order_no =  trim(StrModel::mb_substr2($data['body'], 96, 8));// 截取交易流水号
						
						$DCC_PAY_AMT = trim(StrModel::mb_substr2($data['body'], 194, 16));// 截取交易金额
						
						$DCC_DATE = trim(StrModel::mb_substr2($data['body'], 80, 8));// 截取交易日期
						$param = [
							'account_no' => $no,
							'pay_amount' => $des->des_cbc_encrypt($DCC_PAY_AMT), // 加密交易金额
							'pay_time'	 =>	format_time(time(), 'Y-m-d H:i:s'), // 格式化时间
							'order_no'	 => $des->des_cbc_encrypt($order_no), // 加密流水号
						];
						
						$param = $des->sign($param); // 组建签名后请求参数
						//$param['pay_amount'] = $des->des_cbc_decrypt($param['pay_amount']);
						//return json_encode($param);
						$api = module_config('xianda.pay'); // 请求地址
						
						$return = json_decode(post_curls($api, $param));
						if($return->code == 1){
							$DCC_FLAG = '1111';
							$DCC_RESULT = $return->message;
							$DCC_RESULT = StrModel::addSpace($DCC_RESULT, 60);
							$len = StrModel::addSpace('64', 4);
							$result = $len.$DCC_FLAG.$DCC_RESULT;
						}else{
							//$return['balance'] = $des->des_cbc_decrypt($return['balance']);
							$autoid = OrderModel::where('orderno', $order_no)->value('autoid');
							$DCC_FLAG = '0000';
							$DCC_RESULT = $return->message;
							$DCC_RESULT = StrModel::addSpace($DCC_RESULT, 60);
							$DCC_DATE = StrModel::addSpace($DCC_DATE, 8);
							$DCC_DZDATE = $DCC_DATE;
							$DCC_BANK_LS = StrModel::addSpace($autoid, 8);
							$len = StrModel::addSpace('88', 4);
							$result = $len.$DCC_FLAG.$DCC_RESULT.$DCC_DATE.$DCC_DZDATE.$DCC_BANK_LS;
						}
					}
				break;
				default:
					$DCC_FLAG = '1111';
					$DCC_RESULT = '暂不提供该服务';
					$DCC_RESULT = StrModel::addSpace($DCC_RESULT, 60);
					$len = StrModel::addSpace('64', 4);
					$result = $len.$DCC_FLAG.$DCC_RESULT;
			}
			if(isset($data['id'])){
				StrModel::update(['result' => $result, 'id' => $data['id']]);
			}
			$result = iconv('UTF-8', 'GBK', $result);
			return $result;
		}else{
			$this->success('连接成功');
		}
	}
}
